﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Data.Dynamic;
using EPiServer.Research.MobileCenter.Models;


namespace EPiServer.Research.MobileCenter.Core
{
    public class DDSStorage
    {
        static DDSStorage instance = null;
        static readonly object padlock = new object();

        public DDSStorage()
        {}

        public static DDSStorage Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new DDSStorage();
                    }
                    return instance;
                }
            }
        }


        public void Save(GadgetSettingsForMobile gadgetsettings)
        {
            var settings = LoadSettingsGadget(gadgetsettings.Name);
            settings.IsActive = gadgetsettings.IsActive;
            settings.Name = gadgetsettings.Name;
            settings.UserName = EPiServer.Security.PrincipalInfo.Current.Name;
            storeGadget.Save(settings);
        }

       /* public void Save(SettingsForPageType pagetypesettings)
        {
            var settings = LoadSettings();
            settings.TextContent = pagetypesettings.TextContent;
            settings.StartPointPageID = pagetypesettings.StartPointPageID;
            settings.ImageContent = pagetypesettings.ImageContent;
            settings.MapContent = pagetypesettings.MapContent;
            settings.PageType = pagetypesettings.PageType;
            settings.PageTitle = pagetypesettings.PageTitle;
            settings.UserName = EPiServer.Security.PrincipalInfo.Current.Name;
            storePageGuide.Save(settings);
        }
        */

        public void Save(PageGuideModel pageguidesettings)
        {
            PageGuideModel settings = LoadSettingsPageGuide(pageguidesettings.Name);
            settings.Name = pageguidesettings.Name;
            settings.PageTypeName = pageguidesettings.PageTypeName;
            settings.IsActive = pageguidesettings.IsActive;
            settings.StringProperties = pageguidesettings.StringProperties;
            settings.Title = pageguidesettings.Title;
            settings.XhtmlProperties = pageguidesettings.XhtmlProperties;
            settings.DestionPageID = pageguidesettings.DestionPageID;
            settings.UserName = EPiServer.Security.PrincipalInfo.Current.Name;
            storePageGuide.Save(settings);
        }


        public void DeleteAll()
        {
            storeGadget.DeleteAll();
            storePageGuide.DeleteAll();
        }

        private DynamicDataStore _storeGadget;

        public DynamicDataStore storeGadget {
            get
            {
                if (_storeGadget == null)
                {
                    _storeGadget = DynamicDataStoreFactory.Instance.GetStore(typeof(GadgetSettingsForMobile))
                    ?? DynamicDataStoreFactory.Instance.CreateStore(typeof(GadgetSettingsForMobile));
                    return _storeGadget;
                }
                else
                {
                    return _storeGadget;
                }
            }
        }



        private DynamicDataStore _storePageGuide;

        public DynamicDataStore storePageGuide
        {
            get
            {
                if (_storePageGuide == null)
                {
                    _storePageGuide = DynamicDataStoreFactory.Instance.GetStore(typeof(PageGuideModel))
                    ?? DynamicDataStoreFactory.Instance.CreateStore(typeof(PageGuideModel));
                    return _storeGadget;
                }
                else
                {
                    return _storePageGuide;
                }
            }
        }

        public GadgetSettingsForMobile LoadSettingsGadget(string gadgetname)
        {
            GadgetSettingsForMobile settings = (GadgetSettingsForMobile)storeGadget.Items<GadgetSettingsForMobile>()
                .Where(settingsForGadget => settingsForGadget.UserName == EPiServer.Security.PrincipalInfo.Current.Name && settingsForGadget.Name == gadgetname)
                .FirstOrDefault();

            if (settings == null)
            {
                settings = new GadgetSettingsForMobile();
            }

            return settings;
        }

        public PageGuideModel LoadSettingsPageGuide(string pageguidename)
        {
            PageGuideModel settings = (PageGuideModel)storePageGuide.Items<PageGuideModel>()
                .Where(settingsForPageGuide => settingsForPageGuide.UserName == EPiServer.Security.PrincipalInfo.Current.Name
                && settingsForPageGuide.Name == pageguidename) 
                .FirstOrDefault();

            if (settings == null)
            {
                settings = new PageGuideModel();
            }

            return settings;
        }

        /// <summary>
        /// Get all gadgets
        /// </summary>
        /// <param name="StoreName"></param>
        /// <returns></returns>
        public List<GadgetSettingsForMobile> GetAllGadgets()
        {
            var gadgets = from settings in storeGadget.Items<GadgetSettingsForMobile>()
                          where settings.UserName ==  EPiServer.Security.PrincipalInfo.Current.Name
                          select settings;
            return new List<GadgetSettingsForMobile>(gadgets);
        }

        /// <summary>
        /// Get all active gadgets based
        /// </summary>
        /// <param name="StoreName"></param>
        /// <returns></returns>
        public List<GadgetSettingsForMobile> GetAllActivatedGadgets()
        {
            var gadgets = from settings in storeGadget.Items<GadgetSettingsForMobile>()
                          where settings.IsActive == true
                          && settings.UserName ==  EPiServer.Security.PrincipalInfo.Current.Name
                          select settings;
            return new List<GadgetSettingsForMobile>(gadgets);
        }


        /// <summary>
        /// Get all active pageguides based
        /// </summary>
        /// <param name="StoreName"></param>
        /// <returns></returns>
        public List<PageGuideModel> GetAllActivatedPageGuides()
        {
            var pageguides = from settings in storePageGuide.Items<PageGuideModel>()
                          where settings.IsActive == true
                          && settings.UserName == EPiServer.Security.PrincipalInfo.Current.Name
                          select settings;
            return new List<PageGuideModel>(pageguides);
        }

        /// <summary>
        /// Get all active pageguides based
        /// </summary>
        /// <param name="StoreName"></param>
        /// <returns></returns>
        public List<PageGuideModel> GetAllPageGuides()
        {
            var pageguides = from settings in storePageGuide.Items<PageGuideModel>()
                             where settings.UserName == EPiServer.Security.PrincipalInfo.Current.Name
                             select settings;
            return new List<PageGuideModel>(pageguides);
        }
    }
}
